﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Quanlynhasach.hethongxuly;
using Quanlynhasach.hethongluutru;
using System.IO;
using Quanlynhasach;
using System.Diagnostics;

namespace Quanlynhasach
{
    public partial class frm_KhachHang : Form
    {
        KhachHangDAO khDAO = new KhachHangDAO();
        KhachHangDTO khDTO = new KhachHangDTO();
        public frm_KhachHang()
        {
            InitializeComponent();
        }
        private void HienThiDanhSachKhachHang()
        {
            dgvKhachHang.DataSource = KhachHangDAO.LayDanhSachKhachHang();
        }

        private void KhachHang_Load(object sender, EventArgs e)
        {
            HienThiDanhSachKhachHang();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            khDTO.TenKhachHang = txtTenKhachHang.Text;
            khDTO.DienThoai = txtDienThoai.Text;
            khDTO.DiaChi = txtDiaChi.Text;
            khDTO.Email = txtEmail.Text;
            khDAO.ThemKhachHang(khDTO);
            HienThiDanhSachKhachHang();

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvKhachHang.SelectedRows.Count > 0)
            {
                int maKH = Convert.ToInt32(dgvKhachHang.SelectedRows[0].Cells[0].Value);
                khDTO.MaKhachHang = maKH;
                khDTO.TenKhachHang = txtTenKhachHang.Text;
                khDTO.DienThoai = txtDienThoai.Text;
                khDTO.DiaChi = txtDiaChi.Text;
                khDTO.Email = txtEmail.Text;
                khDAO.XoaKhachHang(khDTO);
                HienThiDanhSachKhachHang();

            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            int maKH = Convert.ToInt32(dgvKhachHang.SelectedRows[0].Cells[0].Value);
            khDTO.MaKhachHang = maKH;
            khDTO.TenKhachHang = txtTenKhachHang.Text;
            khDTO.DienThoai = txtDienThoai.Text;
            khDTO.DiaChi = txtDiaChi.Text;
            khDTO.Email = txtEmail.Text;
            khDAO.CapNhatKhachHang(khDTO);
            HienThiDanhSachKhachHang();

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvKhachHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvKhachHang.SelectedRows.Count > 0)
            {
                DataGridViewRow row = new DataGridViewRow();
                row = dgvKhachHang.Rows[e.RowIndex];
                txtMaKhachHang.Text=row.Cells[0].Value.ToString();
                txtTenKhachHang.Text = row.Cells[1].Value.ToString();
                txtDienThoai.Text = row.Cells[2].Value.ToString();
                txtDiaChi.Text = row.Cells[3].Value.ToString();
                txtEmail.Text = row.Cells[4].Value.ToString();

            }
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            frm_PhieuThuTien.MaKH = txtMaKhachHang.Text;
            this.Close();

        }



       
    }
}
